package com.bbs.dao;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import util.BaseDao;

import com.bbs.bean.BbsTitleInfo;
import com.bbs.bean.BbsUserInfo;
@Repository
public class BbsTitleInfoDao extends BaseDao<BbsTitleInfo> {

	@Override
	public Class getEntityClass() {
		// TODO Auto-generated method stub
		return BbsTitleInfo.class;
	}
	
	public  List<BbsTitleInfo> findAllByPage(int page){
		return getSession().createQuery("from BbsTitleInfo ")
		.setFirstResult(page)
		.setMaxResults(20)
		.list();
	
	}
	
	public int getCountInfo() {
		List list=getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException,
						SQLException {
					return session.createSQLQuery("select count(1) from bbs_title_info").list();
				}
		});
		return ((BigInteger) list.get(0)).intValue();
	}
	
	
	public List<BbsTitleInfo> getByBlock(final Integer pararmValue,final int page){
		List list=getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				return session.createQuery("from BbsTitleInfo where titleBlockid=:pararmValue")
						.setInteger("pararmValue", pararmValue)
						.setFirstResult(page)
						.setMaxResults(20)
						.list();
			}
	});
	return list;
	}
		
	
  public int getCountByBlockId(final Integer pararmValue){
	  List list=getHibernateTemplate().executeFind(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				return session.createSQLQuery("select count(1) from bbs_title_info where title_blockid="+pararmValue)
						.list();
			}
	});
	return ((BigInteger) list.get(0)).intValue();
  }
}
